//
//  BSSBaseNavigationController.m
//  BSSBaseDependTool
//
//  Created by wanglei on 2019/4/15.
//

#import "BSSBaseNavigationController.h"

@interface BSSBaseNavigationController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>
@property (nonatomic, assign) BOOL isPush;
@end

@implementation BSSBaseNavigationController
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController{
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        //关闭系统导航条的现实
        [self setNavigationBarHidden:YES animated:NO];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    __weak BSSBaseNavigationController *weakSelf = self;
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.delegate = weakSelf;
        self.delegate = weakSelf;
    }
}

- (void)pushViewController:(UIViewController *)viewController
                  animated:(BOOL)animated{
    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES ) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers
                  animated:(BOOL)animated{
    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    [super setViewControllers:viewControllers animated:animated];
}

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated{
    if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES ) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    return  [super popToRootViewControllerAnimated:animated];
}

- (NSArray *)popToViewController:(UIViewController *)viewController
                        animated:(BOOL)animated{
    if( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] ) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    // 在VC栈中
    if ([self.viewControllers containsObject:viewController]) {
        return [super popToViewController:viewController animated:animated];
        
    }
    // vc.tabBarController在VC栈中
    if (viewController.tabBarController && [self.viewControllers containsObject:viewController.tabBarController]) {
        return [super popToViewController:viewController.tabBarController animated:animated];
    }
    // 都不在不允许POP
    return nil;
}

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = YES;
    }
    self.isPush = NO;
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPush){
        self.isPush = YES;
    }else{
        self.isPush = NO;
    }
    return nil;
}

/**
 禁止右滑返回
 */
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        if (self.viewControllers.count <= 1 || self.visibleViewController == self.viewControllers.firstObject) {
            return NO;
        }
        if ([self.visibleViewController conformsToProtocol:@protocol(BSSBaseNavigationControllerDelegate)]) {
            id<BSSBaseNavigationControllerDelegate> delegate = (id)self.visibleViewController;
            if ([delegate respondsToSelector:@selector(popGestureEnabled)]) {
                return [delegate popGestureEnabled];
            }
        }
    }
    return YES;
}


@end
